<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @sort-change="sortChange"
    >
      <el-table-column prop="id" align="center" label="ID" width="95" sortable>
        <template v-slot="{row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="任务类型" align="center" width="110">
        <template v-slot="{row}">
          {{ row.job.type === 0 ? '系统' : row.job.type === 1 ? '接口' : '性能' }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="定时任务名称" align="center" width="250">
        <template v-slot="{row}">
          {{ row.job.name }}
        </template>
      </el-table-column>
      <el-table-column label="Cron表达式" align="center" width="120">
        <template v-slot="{row}">
          <span>{{ row.job.cronExpression }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务执行状态" align="center" width="120">
        <template v-slot="{row}">
          <el-tag v-if="row.status === 4" type="danger">失败</el-tag>
          <el-tag v-if="row.status === 5" type="success">成功</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="定时任执行结果" align="left">
        <template v-slot="{row}">
          <span>{{ row.result }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="160">
        <template v-slot="{row}">
          <span> {{ row.createdAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="listQuery.totalRecords>0" :total="listQuery.totalRecords" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { list } from '@/api/project/jobResult'
import { selectAll as testSuiteSelectAll } from '@/api/functional_test/test_case_manage/test_suite'

export default {
  components: { Pagination },
  data() {
    return {
      interfaceList: [], // 接口套件列表
      performanceList: [], // 性能套件列表
      list: null,
      listLoading: true,
      temp: {},
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        totalRecords: 0,
        orders: undefined,
        query: {
        }
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      list(this.listQuery).then(response => {
        this.list = response.data.list
        this.listQuery.totalRecords = response.data.page.totalRecords
        this.listLoading = false
      })

      testSuiteSelectAll({
        projectId: this.$store.getters.projectId,
        caseType: 3
      }).then(response => {
        this.interfaceList = response.data
      })
    },
    filter() {
      this.listQuery.pageNum = 1
      this.fetchData()
    },
    sortChange(column) {
      this.listQuery.orders = [{
        column: column.prop,
        asc: column.order === 'ascending'
      }]
      this.fetchData()
    }
  }
}
</script>
